package gamecfg

import (
	json "encoding/json"
	"fmt"
	"github.com/gogf/gf/v2/os/gctx"
	"github.com/gogf/gf/v2/os/glog"
	"xr-game-server/core/constants/common"
	"xr-game-server/core/xrjson"
)

var rankCfgMap = make(map[uint32]*RankCfg)

const (
	DaySettlement   = 1
	WeekSettlement  = 2
	MonthSettlement = 3
)

// 帮我生成榜单配置文件结构体,根据目录里面的json
type RankCfg struct {
	Id             uint32 `json:"id"`
	SettlementType uint8  `json:"settlementType"`
	Reward         string `json:"reward"`
	Len            int    `json:"len"`
	Asc            bool   `json:"asc"`
}

func loadRankCfg(jsonStr string) {
	err := json.Unmarshal([]byte(jsonStr), &rankCfgMap)
	if err != nil {
		panic(fmt.Errorf("%v 文件加载错误 %v", jsonStr, err))
	}
	glog.Warningf(gctx.New(), "%s数据加载成功,长度:%v", jsonStr, len(rankCfgMap))
}

func GetRankCfgById(id uint32) *RankCfg {
	val, ok := rankCfgMap[id]
	if ok {
		return val
	}
	return nil
}

func GetAllRank() []*RankCfg {
	ret := make([]*RankCfg, common.Zero)
	for _, v := range rankCfgMap {
		ret = append(ret, v)
	}
	return ret
}

func initRank() {
	xrjson.RegLoadJsonHandler("rank.json", loadRankCfg)
}

func (receiver *RankCfg) GetReward() [][]uint64 {
	if receiver.Reward == "" {
		return [][]uint64{}
	}
	var ret [][]uint64
	json.Unmarshal([]byte(receiver.Reward), &ret)
	return ret
}
